#include "GenbankParser.hpp"

class ExampleSink
  : public GenBankSink
{
public:
  ExampleSink() {}
  ~ExampleSink() {}

  void
  OpenKeyword(const char* name, const char* value)
  {
    printf("<keyword name=\"%s\" value=\"%s\">\n",
           name, value);
  }

  void
  CloseKeyword()
  {
    printf("</keyword>\n");
  }

  void
  OpenFeature(const char* name, const char* location)
  {
    printf("<feature>\n");
  }

  void
  CloseFeature()
  {
    printf("</feature>\n");
  }

  void
  Qualifier(const char* name, const char* value)
  {
    printf("<qualifier name=\"%s\" value=\"%s\">\n",
           name, value);
  }

  void
  CodingData(const char* data)
  {
    printf("<code>%s</code>\n", data);
  }
};

int
main(int argc, char** argv)
{
  if (argc < 2)
  {
    fprintf(stderr, "GBPExample usage: GBPExample filename\n");
    return 1;
  }

  TextSource* ts;
  try
  {
    ts = NewBufferedFileSource(argv[1]);
  }
  catch (IOException ioe)
  {
    fprintf(stderr, "Error opening input: %s\n", ioe.what());
    return 1;
  }

  ExampleSink es;

  GenBankParser* gbp = NewGenBankParser();
  gbp->SetSource(ts);
  gbp->SetSink(&es);

  try
  {
    gbp->Parse();
  }
  catch (IOException ioe)
  {
    fprintf(stderr, "Error opening input: %s\n", ioe.what());
    delete gbp;
    delete ts;
    return 1;
  }
  catch (ParserException pe)
  {
    fprintf(stderr, "Parse error: %s\n", pe.what());
    delete gbp;
    delete ts;
    return 1;
  }

  delete gbp;
  delete ts;
  return 0;
}
